#include "Input.h"
#include "Graphic.h"
#include "../World/World.h"
#include "../Menu/MenuLogic.h"
#include "../System/iniparser.h"

int Input_Move;
int Input_Cancel;
int Input_Place;

void Input_Init()
{
    dictionary  *ini;

    /* Lade Config */
    ini = iniparser_load("Config.ini");
    if (ini==NULL) {
        logError("cannot parse file");
        return -1;
    }

    /* Lade die Werte */
    Input_Move = iniparser_getint(ini, "input:move", 2);
    Input_Cancel = iniparser_getint(ini, "input:cancel", 3);
    Input_Place = iniparser_getint(ini, "input:place", 1);

    /* Schließen */
    iniparser_freedict(ini);
}

void Input_Process()
{
    int mX, mY;
    int X,Y;

    /* Highlighted_Tile */
    X = Mouse.X;
    Y = Mouse.Y;
    X += Graphic_GetCamera(0);
    Y += Graphic_GetCamera(1);

    /* Anzeigen */
    mX = XIso( X, Y);
    mY = YIso( X, Y);
    Graphic_Tile(mX, mY, Highlighted_Tile);

    /* Camera Achsen */
    X = Graphic_GetCamera(0);
    Y = Graphic_GetCamera(1);

    /** Cammera Move */
    if(Mouse.MoveCamera) {
        Graphic_Camera(Mouse.cX-Input.X, Mouse.cY-Input.Y);
        Mouse.MoveCamera = 0;
    }

    /** Place */
    if( (Input.Mouse_1 && Input_Place == 1) || (Input.Mouse_2 && Input_Place == 2) || (Input.Mouse_3 && Input_Place == 3) ) {
        /*World_Road(Input.X+X,Input.Y+Y);*/
        if(!Menu_Input(Input.X, Input.Y) && MenuLogic_ID) {
            /* Ist es eine Straße? */
            if(!World_GetRoadID(MenuLogic_ID))
                World_RoadIso(mX, mY);
            else
                /* Als Gebäude setzten */
                World_SetBuilding(mX, mY, MenuLogic_ID);
        }
    }

    /** Move */
    if( (Input.Mouse_1 && Input_Move == 1) || (Input.Mouse_2 && Input_Move == 2) || (Input.Mouse_3 && Input_Move == 3) ) {
        Mouse.MoveCamera = 1;
        Mouse.cX = Mouse.X;
        Mouse.cY = Mouse.Y;
    }

    /** Cancel */
    if( (Input.Mouse_1 && Input_Cancel == 1) || (Input.Mouse_2 && Input_Cancel == 2) || (Input.Mouse_3 && Input_Cancel == 3) ) {
        MenuLogic_ID = 0;
    }


    /** Reset */
    Input.Mouse_1 = 0;
    Input.Mouse_2 = 0;
    Input.Mouse_3 = 0;
}
